/*
 * BanBu_StealthViewController
 * @description 本文件提供隐身模式设置界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供隐身模式设置界面的ViewController
 */

#import "BanBu_StealthViewController.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "BanBu_NavButton.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_MainCell.h"
#import "BanBu_VIPAlert.h"
@interface BanBu_StealthViewController ()

@end

@implementation BanBu_StealthViewController

- (id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)dealloc
{
    [super dealloc];
    [AppComManager cancalHandlesForObject:self];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundView = nil;
    self.tableView.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    self.title = NSLocalizedString(@"stealth", nil);
    
    NSString *hideSetting = [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]valueForKey:@"hideme"];
//    s所有人隐身  n不隐身   h好友隐身  m 陌生人
    if([hideSetting length])
    {
        if([hideSetting isEqualToString:@"n"]){_selectedIndex = 0;}
        else  if([hideSetting isEqualToString:@"s"]){_selectedIndex = 1;}
        else  if([hideSetting isEqualToString:@"h"]){_selectedIndex = 2;}
        else  if([hideSetting isEqualToString:@"m"]){_selectedIndex = 3;}
        else                                  {_selectedIndex = 0;}
    }
    else
    {
        _selectedIndex = 0;
    }
    _willMissIndex = _selectedIndex;
    
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.tableView.backgroundView = img;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{   
    return NSLocalizedString(@"stealthHelpTip", nil);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BanBu_MainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[BanBu_MainCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    
    if([ThemeManager getThemeImage:@"tablecell_backimage.png"])
    {
        cell.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:@"tablecell_backimage.png"]];
        cell.textLabel.backgroundColor = [UIColor clearColor];
    }
//    else
//        cell.backgroundColor = nil;
    if(indexPath.row == _selectedIndex)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    if(!indexPath.row)
    {
        cell.textLabel.text = NSLocalizedString(@"visibleAll", nil);
    }
    else if(indexPath.row == 1)
    {
        cell.textLabel.text = NSLocalizedString(@"stealthAll", nil);
    }
    else if(indexPath.row ==2)
    {
        cell.textLabel.text = NSLocalizedString(@"stealthFriend", nil);
    }
    else
    {
        cell.textLabel.text = NSLocalizedString(@"stealthStranger", nil);
        }

    return cell;
    
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == _selectedIndex)
        return;
    BanBu_MainCell *cell = (BanBu_MainCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0]];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell = (BanBu_MainCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    _willMissIndex = _selectedIndex;
    _selectedIndex = indexPath.row;
    [TKLoadingView showTkloadingAddedTo:self.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
    NSMutableDictionary *reDic = [NSMutableDictionary dictionaryWithCapacity:1];
//    s所有人隐身  n不隐身   h好友隐身  m 陌生人
    switch (_selectedIndex) {
        case 0:
            [reDic setValue:@"n" forKey:@"hideme"];
            break;
        case 1:
            [reDic setValue:@"s" forKey:@"hideme"];
            break;
        case 2:
            [reDic setValue:@"h" forKey:@"hideme"];
            break;
        case 3:
            [reDic setValue:@"m" forKey:@"hideme"];
            break;
        default:
            break;
    }
    [AppComManager cancalHandlesForObject:self];
    [AppComManager getBanBuData:BanBu_Set_Online_Hideme par:reDic delegate:self];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    [TKLoadingView dismissTkFromView:self.view animated:YES afterShow:0];
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:YES afterShow:0];
    
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"noData", YES) activityAnimated:NO duration:0.5];
        BanBu_MainCell *cell = (BanBu_MainCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        cell = (BanBu_MainCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_willMissIndex inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        _selectedIndex = _willMissIndex;
        return;
    }
    else
    {
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"modifySucceed", nil) activityAnimated:NO duration:1];
            _willMissIndex = _selectedIndex;
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
            NSMutableDictionary *setting = [NSMutableDictionary dictionaryWithDictionary:[dic valueForKey:@"settings"]];
            NSString *hideSetting = nil;
            switch (_selectedIndex) {
                case 0:
                    hideSetting = @"n";
                    break;
                case 1:
                    hideSetting = @"s";
                    break;
                case 2:
                    hideSetting = @"h";
                    break;
                case 3:
                    hideSetting = @"m";
                    break;
                default:
                    break;
            }
            [setting setValue:hideSetting  forKey:@"hideme"];
            [dic setValue:setting forKey:@"settings"];
            [UserDefaults setValue:dic forKey:MyAppDataManager.useruid];
            
        }
        else
        {
            BanBu_MainCell *cell = (BanBu_MainCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0]];
            cell.accessoryType = UITableViewCellAccessoryNone;
            
            cell = (BanBu_MainCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_willMissIndex inSection:0]];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            _selectedIndex = _willMissIndex;

            
            if([[resDic allKeys] indexOfObject:@"error"] != NSNotFound)
            {
                if([[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"userlevel"] integerValue]==0)
                {
                    [[BanBu_VIPAlert shareInstance] GoVIPApplyViewAlertFor:self title:NSLocalizedString(@"noticeNotice", nil) message:[MyAppDataManager IsInternationalLanguage:[resDic valueForKey:@"error"]]];
                }
//                [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:[MyAppDataManager IsInternationalLanguage:[resDic valueForKey:@"error"]] activityAnimated:NO duration:1];
            }
        }
    }
    
}
@end

